package com.jrx.anytxn.param.controller.investor;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.PrInvestorInterestRateReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorInterestRateRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorInterestRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 出资方利率表 控制器
 *
 * @author 黄秋平
 * @date 2020-05-18 10:16
 **/
@Api(tags = "出资方利率参数api接口")
@RestController
@RequestMapping(value = "/param/investor/investorInterest")
public class PrInvestorInterestRateController {

    private static final Logger logger = LoggerFactory.getLogger(PrInvestorInterestRateController.class);
    
    @Resource
    IPrInvestorInterestRateService iPrInvestorInterestRateService;
    
    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和利率表ID查询出资方利率表信息", notes = "根据租户ID和利率表ID查询出资方利率表信息")
    @GetMapping(value = "/{tenantId}/{investorInterestId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "investorInterestId", value = "出资方利率表ID", required = true)
    })
    public TxnRespResult<PrInvestorInterestRateRes> getByTenIdAndInterestId(@NotNull @PathVariable String tenantId, @PathVariable String investorInterestId) {
        TxnRespResult<PrInvestorInterestRateRes> result = new TxnRespResult<>();
        PrInvestorInterestRateRes res;
        try {
            res = iPrInvestorInterestRateService.findByTenIdAndInterestId(String.valueOf(infoService.getUser().getProjectId()), investorInterestId);
            if (res.getBaseRate() != null && res.getBaseRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setBaseRate(res.getBaseRate().multiply(BigDecimal.valueOf(100)));
            }
            if (res.getBaseRatePercent() != null && res.getBaseRatePercent().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setBaseRatePercent(res.getBaseRatePercent().multiply(BigDecimal.valueOf(100)));
            }
            if (res.getTaxRate() != null && res.getTaxRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setTaxRate(res.getTaxRate().multiply(BigDecimal.valueOf(100)));
            }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询出资方利率表信息", notes = "分页查询出资方利率表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrInvestorInterestRateRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrInvestorInterestRateRes>> result = new TxnRespResult<>();
        TxnPage<PrInvestorInterestRateRes> response;
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        response = iPrInvestorInterestRateService.findAllPageByTenId(pageNum, pageSize, tenantId);
        result.getSuccess(response);

        return result;
    }


    @ApiOperation(value = "新增出资方利率表信息", notes = "新增出资方利率表信息")
    @PostMapping("/")
    public TxnRespResult<PrInvestorInterestRateRes> createInvestorInterestRate(@RequestBody PrInvestorInterestRateReq prInvestorInterestRateReq) {
        PrInvestorInterestRateRes res;
        TxnRespResult<PrInvestorInterestRateRes> result = new TxnRespResult<>();
        try {
            prInvestorInterestRateReq.setBaseRatePercent(prInvestorInterestRateReq.getBaseRatePercent());
            prInvestorInterestRateReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prInvestorInterestRateReq.setUpdateBy(infoService.getUser().getUserName());
            if (prInvestorInterestRateReq.getBaseRate() != null && prInvestorInterestRateReq.getBaseRate().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorInterestRateReq.setBaseRate(prInvestorInterestRateReq.getBaseRate().divide(BigDecimal.valueOf(100)));
            }
            if (prInvestorInterestRateReq.getBaseRatePercent() != null && prInvestorInterestRateReq.getBaseRatePercent().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorInterestRateReq.setBaseRatePercent(prInvestorInterestRateReq.getBaseRatePercent().divide(BigDecimal.valueOf(100)));
            }
            if (prInvestorInterestRateReq.getTaxRate() != null && prInvestorInterestRateReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorInterestRateReq.setTaxRate(prInvestorInterestRateReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorInterestRateService.addInterestTable(prInvestorInterestRateReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "编辑出资方利率表信息", notes = "编辑出资方利率表信息")
    @PutMapping("/")
    public TxnRespResult<PrInvestorInterestRateRes> modifyInvestorInterestRate(@RequestBody PrInvestorInterestRateReq prInvestorInterestRateReq) {
        TxnRespResult<PrInvestorInterestRateRes> result = new TxnRespResult<>();
        PrInvestorInterestRateRes res;
        try {
            if (prInvestorInterestRateReq.getBaseRate() != null && prInvestorInterestRateReq.getBaseRate().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorInterestRateReq.setBaseRate(prInvestorInterestRateReq.getBaseRate().divide(BigDecimal.valueOf(100)));
            }
            if (prInvestorInterestRateReq.getBaseRatePercent() != null && prInvestorInterestRateReq.getBaseRatePercent().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorInterestRateReq.setBaseRatePercent(prInvestorInterestRateReq.getBaseRatePercent().divide(BigDecimal.valueOf(100)));
            }
            if (prInvestorInterestRateReq.getTaxRate() != null && prInvestorInterestRateReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorInterestRateReq.setTaxRate(prInvestorInterestRateReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            prInvestorInterestRateReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrInvestorInterestRateService.modifyInterestTable(prInvestorInterestRateReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "删除出资方利率表信息", notes = "根据id删除出资方利率表信息")
    @DeleteMapping(value = "/{tenantId}/{investorInterestId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户Id", required = true),
            @ApiImplicitParam(name = "investorInterestId", value = "出资方利率表ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndInterestId(@NotNull @PathVariable String tenantId, @PathVariable String investorInterestId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iPrInvestorInterestRateService.removeByTenIdAndInterestId(String.valueOf(infoService.getUser().getProjectId()), investorInterestId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "通过出资方利率ID，及机构获取出资方利率信息", notes = "通过出资方利率ID，及机构获取出资方利率信息")
    @GetMapping(value = "/investorInterestId/{investorInterestId}/orgId/{orgId}")
    public TxnRespResult<PrInvestorInterestRateRes> getByInterestIdAndOrgId(@NotNull @PathVariable String investorInterestId, @NotNull @PathVariable String orgId) {
        TxnRespResult<PrInvestorInterestRateRes> result = new TxnRespResult<>();
        PrInvestorInterestRateRes PrInvestorInterestRateRes;
        try {
            String tenantId= String.valueOf(infoService.getUser().getProjectId());
            PrInvestorInterestRateRes = iPrInvestorInterestRateService.findByInterestId(investorInterestId, orgId,tenantId);
            result.getSuccess(PrInvestorInterestRateRes);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            result.getFail(TxnRespCode.ERROR.getCode(), e.getMessage());
        }
        return result;
    }

}
